<?php

namespace App\Http\Controllers\v1;


use App\Models\Area;
use App\Models\Banner;
use App\Models\Family;
use App\Models\Project;
use App\Models\SmsCode;
use App\Models\Team;
use App\Models\User;
use App\Models\UserApply;
use App\Models\UserCollection;
use App\Models\UserProject;
use App\Models\Volunteer;
use App\Models\VolunteerGoodAt;
use App\Models\WechatTempMsg;
use EasyWeChat\Core\Exception;
use Illuminate\Http\Request;
use App\Libs\IdCard;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;
use Hashids\Hashids;

class UserApplyController extends Controller
{

    public function create(Request $request)
    {
        $rules = ['project_id'=>'required|numeric',  'reward_time'=>'required|numeric|min:0',  'content'=>'required',  'attachment'=>'array',  ];
        $validator = Validator::make(Input::all(), $rules);
        if($validator->fails())
        {
            $errors = $validator->errors();
            $this->setStatus(10001);
            $this->setMsg(implode(' ', $errors->all()));

            return $this->response();
        }
        $userInfo = User::where('type', 1)->where('token', $this->getToken())->first();
        $projectId = $request->input('project_id');
        $rewardTime = $request->input('reward_time');
        $content = $request->input('content');
        $attachment = $request->input('attachment');

        $userProject = UserProject::where('user_id', $userInfo->id)->where('project_id', $projectId)->where('verify_status', 1)->first();
        if(empty($userProject))
        {
            $this->setStatus(10001);
            $this->setMsg('非法的项目ID');

            return $this->response();
        }

        $data['apply_user_id'] = $userInfo->id;
        $data['team_id'] = $userProject->project->team_id;
        $data['org_id'] = $this->getOrdId();
        $data['project_id'] = $projectId;
        $data['reward_time'] = $rewardTime;
        $data['content'] = $content;
        $data['attachment'] = $attachment;
        $data['verify_status'] = 0;

        $userApply = new UserApply();
        $userApply->fill($data)->save();

        //微信模版消息，团队管理员收到用户申请补录时长
        WechatTempMsg::add(52, ['user_apply_id'=>$userApply->id,]);


        return $this->response();
    }

    public function index(Request $request)
    {
        $userInfo = User::where('type', 1)->where('token', $this->getToken())->first();

        $nodes = UserApply::with('project')->where('apply_user_id', $userInfo->id);
        $nodes = $nodes->orderBy('id', 'desc');

        $lists = $nodes->get()->toArray();
        $list = array_map(function($item){

            $tmp = UserApply::format($item);
            $tmp['project'] = Project::format($item['project']);
            return $tmp;
        }, $lists);

        $data['list'] = $list;
        $this->setData($data);
        return $this->response();
    }

}
